﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenWeb.Dal.Models;
using OpenWeb.Dal.Interfaces;
using OpenWeb.Dal.Repository;
using System.Linq.Expressions;

namespace OpenWeb.Dal.Services
{
  
  public class ContactService : ServiceBase<IContacts>, IContactsService
    {
        public ContactService()
            : this(new ContactRepository())
        {
        }

        private IContactRepository _repository;
        public ContactService(IContactRepository pRepository)
        {
          _repository = pRepository ?? new ContactRepository();
        }

        protected override Type LogPrefix
        {
            get { return this.GetType(); }
        }

        public GenericListModel<Contact> GetList(int PageSize, int CurrentPage)
        {
            try
            {
                if ((CurrentPage == 0) || (PageSize == 0))
                    return null;

                IQueryable<Contact> query = _repository.GetQueryable();
                var model = new GenericListModel<Contact>();
                model.TotalItemCount = query.Count();
                if (model.TotalItemCount == 0)
                    return null;
                model.TotalPageCount = (int)Math.Ceiling((double)model.TotalItemCount / (double)PageSize);
                //if (model.TotalPageCount != 1)
                //    query = query.OrderBy(x => x.DocumentName).Skip((CurrentPage - 1) * PageSize).Take(PageSize);

                model.ThisPageItemCount = query.Count();
                model.ObjList = new List<Contact>();

                return model;
            }
            catch (System.Exception e)
            {
                //this.LogError("Error getting the document list", e);
                return null;
            }
        }

        //public List<DocumentFileViewModel> GetDocumentFileList(int DocumentId)
        //{
        //    try
        //    {
        //        if (DocumentId == 0)
        //            return null;

        //        IQueryable<Document> query = _repository.GetQueryable();

        //        FileVersion maxVersion = null;
        //        AutoMapper.Mapper.CreateMap<DocFile, DocumentFileViewModel>()
        //            .BeforeMap((s, d) => maxVersion = s.FileVersions.OrderBy(x => x.Version).FirstOrDefault())
        //            .ForMember(dest => dest.ID, opt => opt.MapFrom(src => src.FileId))
        //            .ForMember(dest => dest.VersionNumber, opt => opt.MapFrom(src => maxVersion.Version.ToString("V 0.0.0")))
        //            .ForMember(dest => dest.AddedDate, opt => opt.MapFrom(src => maxVersion.AddedDate))
        //            .ForMember(dest => dest.AddedBy, opt => opt.MapFrom(src => maxVersion.AddedByName))
        //            .ForAllMembers(x => x.NullSubstitute("No Record Found"));

        //        DocFile[] docFiles = query.SingleOrDefault(x => x.DocumentId == DocumentId).DocFiles.ToArray();
        //        List<DocumentFileViewModel> dfList = new List<DocumentFileViewModel>();
        //        dfList = AutoMapper.Mapper
        //            .Map(docFiles, dfList);

        //        return dfList;
        //    }
        //    catch (Exception e)
        //    {
        //        this.LogError("Error getting Document File List", e);
        //        return null;
        //    }
        //}

        public Contact GetSingle(Expression<Func<Contact, bool>> whereCondition)
        {
            throw new NotImplementedException();
        }

        public void Add(Contact entity)
        {
            throw new NotImplementedException();
        }

        public void Delete(Contact entity)
        {
            throw new NotImplementedException();
        }

        public void Update(Contact entity)
        {
            throw new NotImplementedException();
        }

        public IList<Contact> GetAll(Expression<Func<Contact, bool>> whereCondition)
        {
            throw new NotImplementedException();
        }

        public IList<Contact> GetAll()
        {
            throw new NotImplementedException();
        }

        public IQueryable<Contact> Query(Expression<Func<Contact, bool>> whereCondition)
        {
            throw new NotImplementedException();
        }

        public long Count(Expression<Func<Contact, bool>> whereCondition)
        {
            throw new NotImplementedException();
        }

        public long Count()
        {
            throw new NotImplementedException();
        }


        public void Attach(Contact entity)
        {
          throw new NotImplementedException();
        }

        public new IQueryable<Contact> GetQueryable()
        {
          throw new NotImplementedException();
        }

        public new System.Data.Objects.IObjectSet<Contact> ObjectSet
        {
          get { throw new NotImplementedException(); }
        }

        public new System.Data.Entity.IDbSet<Contact> DbSet
        {
          get { throw new NotImplementedException(); }
        }
    }
}
